// pages/attendancecount/attendancecount.js
const {
    imgConfig, config
} = require('../../utils/config.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgUrl: `${imgConfig}`,
        statusBarHeight: app.globalData.statusBarHeight,
        attendstatus:true,
        leavestatus:true,
        earlystatus:true,
        latestatus:true,
    },

    changeattend(){
        this.setData({
            attendstatus:!this.data.attendstatus
        })
    },
    changeleave(){
        this.setData({
            leavestatus:!this.data.leavestatus
        })
    },
    changelate(){
        this.setData({
            latestatus:!this.data.latestatus
        })
    },
    changeearly(){
        this.setData({
            earlystatus:!this.data.earlystatus
        })
    },

    back: function () {
        wx.navigateBack({});
    },
    goAttendance(){
        wx.navigateTo({
          url: '/pages/attendance/attendance',
        })
    },
    getAttendDateList(currentMonth,currentYear){
        var that = this 
        wx.request({
          url: `${config}/wechat/wxclock/stuAttendList`,
          method:'get',
          data:{
              stuId: app.globalData.userInfo.id,
              currentYear:currentYear,
              currentMonth:currentMonth
          },
          success(resp){
              console.log(resp)
              that.setData({
                  attendDate:resp.data.stuAttendList.stuAttendList,
                  attendCount:resp.data.stuAttendList.stuAttendList.length
              })
          }
        })
    },
  //   getAttendDateList(currentMonth,currentYear){
  //     var that = this 
  //     wx.request({
  //       url: `${config}/wechat/wxAttendance/stuDateList`,
  //       method:'get',
  //       data:{
  //           stuId: app.globalData.userInfo.id,
  //           currentYear:currentYear,
  //           currentMonth:currentMonth
  //       },
  //       success(resp){
  //           console.log(resp)
  //           that.setData({
  //               attendDate:resp.data.stuDateList.attendDate,
  //               attendCount:resp.data.stuDateList.attendDate.length,
  //               lateDate:resp.data.stuDateList.lateDate,
  //               lateCount:resp.data.stuDateList.lateDate.length,
  //               earlyDate:resp.data.stuDateList.earlyDate,
  //               earlyCount:resp.data.stuDateList.earlyDate.length,
  //           })
  //       }
  //     })
  // },
    getLeaveDate(currentMonth,currentYear){
        var that = this
        wx.request({
          url: `${config}/wechat/wxAttendance/getAttendanceList`,
          method:'get',
          data:{
              stuId: app.globalData.userInfo.id,
              currentMonth:currentMonth,
              currentYear:currentYear
          },    
          success(resp){
            let leaveList =[]
                for (let i = 0; i < resp.data.countMonth.length; i++) {
                if (resp.data.countMonth[i].beginDate !=resp.data.countMonth[i].endDate) {
                  let a = resp.data.countMonth[i]['endDate'].substring(8)-resp.data.countMonth[i]['beginDate'].substring(8)
                  for (let j = 0; j < a; j++) {
                    // 定义时间
                    var diyDate = new Date(resp.data.countMonth[i]['beginDate'])
                    //加1天
                    let date = new Date(diyDate.setDate(diyDate.getDate() + j));
                    // 拼接日期 yyyy-MM-dd 注意月份要 + 1
                    let begendList = date.getFullYear() + "-" + ((date.getMonth() + 1) > 9 ? date.getMonth() + 1 : "0" + (date.getMonth() + 1)) + "-" + (date.getDate() > 9 ? date.getDate() : "0" + date.getDate());
                    leaveList.push({
                      applyDate:begendList
                    })
                  }
                }else{
                  leaveList.push({
                    applyDate:resp.data.countMonth[i]['applyDate']
                  })
                }
              }  
              that.setData({
                  leaveCount:leaveList.length,
                  leaveDate:leaveList
              })
          }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var currentMonth = options.currentMonth
        var currentYear = options.currentYear
        this.getAttendDateList(currentMonth,currentYear)
        this.getLeaveDate(currentMonth,currentYear)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        this.setData({
            avatar: app.globalData.userInfo.photo, //头像
            stuName: app.globalData.userInfo.stuName, //姓名
            phoneNumber: app.globalData.userInfo.stuTel, //手机号
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})